package com.oauth.facebook.client.ui.tabs;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.oauth.components.client.rpc.OAuthAsnyCallBack;
import com.oauth.facebook.client.beans.FacebookUser;
import com.oauth.facebook.client.rpc.FacebookRemoteServiceAsync;
import com.oauth.facebook.client.ui.ConnectionPanel;
import com.oauth.facebook.client.ui.FacebookLoadingPanel;

public class MyConnectionsTab extends Composite {

	private static MyConnectionsTabUiBinder uiBinder = GWT.create(MyConnectionsTabUiBinder.class);

	interface MyConnectionsTabUiBinder extends UiBinder<Widget, MyConnectionsTab> {
	}

	@UiField
	VerticalPanel connectionsPanel;

	@UiField
	FacebookLoadingPanel loadingPanel;

	private FacebookRemoteServiceAsync service;

	public MyConnectionsTab(FacebookRemoteServiceAsync pService) {
		service = pService;
		initWidget(uiBinder.createAndBindUi(this));
	}

	public void init() {
		loadingPanel.setVisible(true);

		connectionsPanel.clear();

		service.getConnections(new OAuthAsnyCallBack<List<FacebookUser>>() {

			@Override
			public void onSuccess(List<FacebookUser> pResult) {
				if (pResult.isEmpty()) {
					connectionsPanel.add(new Label("You don't have any connections"));
				} else {
					for (FacebookUser user : pResult) {
						connectionsPanel.add(new ConnectionPanel(user));
					}
				}
				loadingPanel.setVisible(false);
			}
		});
	}
}
